package com.xrit.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xrit.sunnyday.constant.MessageConstant;
import com.xrit.sunnyday.entity.PageResult;
import com.xrit.sunnyday.entity.QueryPageBean;
import com.xrit.sunnyday.entity.Result;
import com.xrit.sunnyday.pojo.Role;
import com.xrit.sunnyday.service.RoleService;
import org.springframework.web.bind.annotation.*;

/**
 * @author Daoying
 * creationTime: 2022/10/25 14:37
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    RoleService roleService;


    /*
     * 分页
     * */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult list = roleService.findPage(queryPageBean);

        return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, list);
    }

    /**
     * 添加角色与权限
     */
    @PostMapping("/RoleAdd")
    public Result RoleAdd(@RequestBody Role role, Integer[] permissionIds, Integer[] menus) {
        roleService.RoleAdd(role, permissionIds, menus);
        Result result = new Result(true, "角色添加成功！");
        return result;
    }

    /*
     * 通过id查询
     * */
    @PostMapping("/findById4Edit")
    public Result findById4Edit(int id) {
        Role role = roleService.findById4Edit(id);

        return new Result(true, MessageConstant.EDIT_USER_SUCCESS, role);
    }

    /**
     * 删除角色信息
     *
     * @param id
     * @return
     */
    @PostMapping("/RoleDelete/{id}")
    public Result RoleDelete(@PathVariable Integer id) {
        roleService.RoleDelete(id);
        Result result = new Result(true, "删除成功！");
        return result;
    }

    /**
     * 根据角色ID修改角色信息
     *
     * @param
     * @return
     */
    @PostMapping("/RoleUpdate")
    public Result RoleUpdate(@RequestBody Role role, Integer[] permissionIds, Integer[] menus) {
        roleService.RoleUpdate(role, permissionIds, menus);
        Result result = new Result(true, "修改角色信息！");
        return result;
    }
}
